package player.miniplayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;


public class SimpleMplayer {
	static String mplayerPath="mplayer\\mplayer.exe";
	static String [] cmd ;
	static int length=0;
	public static int playOffset=0;
	static Process proc ;
	static Thread t1;
	static Thread t2;
	public  static int lastOffset;
	public static boolean isPause=false;
	ArrayList<Integer> pList=new ArrayList<Integer>();
	static long wid=0;
	static String mediaPath;
	public static boolean isPlay=false;
	
	
	public static void setMediaPath(String s){
		mediaPath=s;
	}
	
	public static void startMplayer(){
		 cmd = new String[] {
					mplayerPath,
					"-vo","directx:noaccel",
					"-identify", 
					"-slave", 
					"-wid",String.valueOf(wid),
					"-colorkey", "0x202020",
					"-nokeepaspect",
					"-osdlevel", String.valueOf(0),
					"-ss",String.valueOf(0),
					"-nosound",
					mediaPath };
		 
		try {
			proc = Runtime.getRuntime().exec(cmd);
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
	}
	
	public static void setWid(long n){
		wid=n;
	}
	
	public static void playMedia() {
		stopPlay();
		startMplayer();
		isPlay=true;
		isPause=false;
		Class<?> cl;

		final InputStream is1 = proc.getErrorStream();
		final InputStream is2 = proc.getInputStream();
		final Runnable errorReader = new Runnable() {
			public void run() {
				try {
					final BufferedReader lReader = new BufferedReader(new InputStreamReader(is1));
					for (String l = lReader.readLine(); l != null ; l = lReader.readLine()) {
						 //System.out.println("ERROR "+l);
					}
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		};
		
		
		final Runnable standReader = new Runnable() {
			public void run() {
				try {
					final BufferedReader lReader = new BufferedReader(new InputStreamReader(is2));
					for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {
						//System.out.println("OUT "+l);
						if (l.length() >= 10) {
							String s2 = l.substring(0, 9);
							if (s2.equals("ID_LENGTH")) {
								int index = l.indexOf(".");
								String s1 = l.substring(10,index);
								length = Integer.valueOf(s1);
							}
						}
						if(l.length()>70&&l.startsWith("V:")){
							int index = l.indexOf(".");
							String s3 = l.substring(2, index);
							int index2 = 0;
							while (true) {
								int index3 = s3.indexOf(" ", index2);
								if (index3 != -1) {
									index2 = index3 + 1;
								} else {
									break;
								}
							}
							String s4 = s3.substring(index2);
							playOffset = Integer.valueOf(s4);
						}
						
					}
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		};
		
		t1 = new Thread(errorReader);
		t2 = new Thread(standReader);
		t1.start();
		t2.start();
		
		
		Thread waitThread=new Thread (){
			@Override
			public void run(){
				try {
					proc.waitFor();
					t1.interrupt();
					t2.interrupt();

					isPlay=false;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		};
		waitThread.start();
		
	}
	
	public static void seekto(int value) {
		isPause=false;
		if (proc != null) {
			PrintStream s = new PrintStream(proc.getOutputStream());
			String command = "seek " + value + " 2\n";
			s.print(command);
			s.flush();
		}else{
			startMplayer();
		}
	}
	
	public static void pausePlay() {
		if (proc != null) {
			PrintStream s = new PrintStream(proc.getOutputStream());
			s.print("pause\n");
			s.flush();
			isPause=!isPause;
		}
		
	}
	
	public static void continuePlay(){
		if(isPlay&isPause){
			if (proc != null) {
				PrintStream s = new PrintStream(proc.getOutputStream());
				s.print("pause\n");
				s.flush();
				isPause=!isPause;
			}
		}
		
	}
	
	public static void stopPlay() {
		{
			if (proc != null) {
				PrintStream s = new PrintStream(proc.getOutputStream());
				s.print("stop\n");
				s.flush();
			}
			length=0;
			playOffset = 0;
			isPlay = false;
		}
	}
	
	static String  getNTime(int time){
		int h=time/3600;
		int m=time/60-h*60;
		int s=time%60;
		String h1=Integer.toString(h);
		String m1=Integer.toString(m);
		String s1=Integer.toString(s);
		if(h1.length()==1){
			h1="0"+h1;
		}
		if(m1.length()==1){
			m1="0"+m1;
		}
		if(s1.length()==1){
			s1="0"+s1;
		}
		String ds=h1+"."+m1+"."+s1;
		return ds;
	}
	
	public static void exit(){
		if(proc!=null){
			proc.destroy();
			t1.interrupt();
			t2.interrupt();
		}
		
	}




}
